#include "Goomba.h"
#include "ResourceManager.h"
#include <d3dx9.h>

Goomba::Goomba(int id,float x, float y, int type, int state, int direction)
{
	_checkTurn = false;
	_sprite = new Sprite(_spriteHandler, ResourceManager::GetInstance()->GetTexture(ResourceManager::GetInstance()->TextureName.Goomba),150, 0);
	_id = id;
	_x = x;
	_y = y;
	_type = type;
	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();
	_vx = (float)GOOMBA_VX;
	_vy = 0;
	_accelY = 0;
	_xPre = x;
	_yPre = y;
	_state = state;
	_sprite->SetFrame(0,2);

	_timeDie = 0;

	_isGravity = true;


	this->_state = state;
	_direction = direction;
	if (_state == ES_ACTIVING)
	{
		if (direction == 1)			//sang trai
		{
			_vx = -(float)GOOMBA_VX;
		} 
		else if (direction == 2)	//sang phai
		{
			_vx = (float)GOOMBA_VX;
		}
	} 
}


Goomba::~Goomba(void)
{
	if (_sprite)
	{
		delete _sprite;
	}
	
}

void Goomba::Update(list<Object *> *_list, float time)
{
	switch (_state)
	{
	case ES_ACTIVING:
		Move(_list, time);//Move(staObjs, time);
		_sprite->Update(time);
		if (_y <= 0)	//goomba roi xuong ho, _height = 0
		{
			changeState(ES_REMOVE);
		}
		break;
	case ES_DIED:
		_timeDie += time;
		if (_timeDie > 900)
		{
			changeState(ES_REMOVE);
		}
		_sprite->Update(time);
		break;
	case ES_FALL:
		_x += _vx * time;
		_vy -= _accelY * time;
		_y += _vy * time + 1/2 * _accelY * time *time;
		if (_y <= 0)	//goomba roi xuong ho, _height = 0
		{
			changeState(ES_REMOVE);
		}

		_sprite->Update(time);
		break;
	}
}


void Goomba:: Render(int vpX, int vpY)
{
	if (_state != ES_REMOVE)
	{
		_sprite->Render(_x, _y, vpX, vpY);
	}
}

void Goomba ::Move(list<Object*>* _list, float time)
{
	Object* obj;
	list<Object*>::iterator i;
	_width = _sprite->getTexture()->getWidth();
	_height = _sprite->getTexture()->getHeight();
	
	//Xet va cham theo truc X
	_x += _vx * time;
	

	//Xet va cham theo truc Y
	if (_isGravity == false)
	{
		_accelY = 0;
	}
	else
	{
		_accelY = GOOMBA_GRAVITY;
	}	
	_vy -= _accelY * time;
	_y += _vy * time + 1/2.0 * _accelY * time * time;
	
	for (i = _list->begin(); i != _list->end(); i++)
	{
		obj = *i;
		if (obj->getObjectType() == OBJECT_LAND)
		{
			//Box box = this->getBox(time);
			Box box(this->_xPre,
				this->_yPre - this->_height, 
				_width, 
				_height, 
				_vx * time, 
				_vy * time + 1.0f/2.0f * _accelY * time * time);

			//Box block = obj->getBox(time);
			Box block(obj->getPosX(),
				obj->getPosY() - obj->getHeight(),// - 0.11,
				obj->getWidth(),
				obj->getHeight()
				);

			Box boxphase = Collision::_GetSweptBroadphaseBox(box);
			if (Collision::_AABBCheck(boxphase, block))
			{
				float normalx = 0;
				float normaly = 0;
				float collision_time = Collision::_SweptAABB(box, block, normalx, normaly);
				if (0 < collision_time && collision_time < 1)
				{
					
					if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
					{
						_x = _xPre + _vx * collision_time;
						_accelY = 0;
						_vx = -_vx;
					} 
					else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
					{
						_x = _xPre + _vx * collision_time;
						_accelY = 0;
						_vx = -_vx;

					}
					else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
					{
					}
					else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
					{
						

						_y = _yPre + _vy * collision_time + 1.0f/2.0f * _accelY * collision_time *collision_time + 0.5;
						_accelY = 0;
						_vy = 0;
					}

					if (this->_yPre - this->_height <= obj->getPosY() + 1.5)
					{
						_isGravity = false;
					}
					else
					{
						_isGravity = true;
					}
					 
				}
				else
				{
				
				}
			}
			else
			{
				if (this->_yPre - this->_height > obj->getPosY() + 1.5)
				{
					_isGravity = true;
				}
				
			}
			 
		}
	}

	if(_vx > 0)
	{
		TurnRight();
	}
	else
	{
		TurnLeft();
	}

	_xPre = _x;
	_yPre = _y;

	if (_vx >= 0)
	{
		_direction = 2;
	} 
	else
	{
		_direction = 1;
	}
	
}
void Goomba :: TurnLeft()
{
	_checkTurn = true;
	_vx = (float)-GOOMBA_VX;
}


void Goomba :: TurnRight()
{
	_checkTurn = false;
	_vx = (float)GOOMBA_VX;
}

void Goomba::changeState(int state)
{
	_state = state;
	switch (_state)
	{
	case ES_ACTIVING:
		_vx = GOOMBA_VX;
		_accelY = GOOMBA_GRAVITY;
		break;

	case ES_FALL:
		if (_checkTurn == false)
		{
			_sprite->SetFrame(2,2);
		} 
		else
		{
			_sprite->SetFrame(2,2);
		}
		_vy = 1.5;
		_accelY = GOOMBA_GRAVITY;

		break;
	case ES_DIED:
		if (_checkTurn == false)
		{
			_sprite->SetFrame(2,2);
		} 
		else
		{
			_sprite->SetFrame(2,2);
		}
		
		_vx = 0;
		break;
	}
}
OBJECT_TYPE Goomba::getObjectType()
{
	return OBJECT_ENEMY;
}